package org.giovanolli.mumi.controller.business;

import org.giovanolli.mumi.controller.AbstractController;
import org.giovanolli.mumi.controller.AbstractModel;
import org.giovanolli.mumi.entities.Patient;
import org.giovanolli.mumi.entities.PatientPrepaid;
import org.giovanolli.mumi.entities.Phone;
import org.giovanolli.mumi.entities.filter.CityFilter;
import org.giovanolli.mumi.entities.filter.PatientFilter;
import org.giovanolli.mumi.entities.filter.PrepaidFilter;
import org.giovanolli.mumi.entities.filter.SpecialistFilter;
import org.giovanolli.mumi.service.CityService;
import org.giovanolli.mumi.service.PatientService;
import org.giovanolli.mumi.service.PrepaidService;
import org.giovanolli.mumi.service.Service;
import org.giovanolli.mumi.service.SpecialistService;
import org.primefaces.event.FlowEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("patientController")
public class PatientController extends AbstractController<Patient> {

	@Autowired
	private PatientService service;

	@Autowired
	private CityService cityService;

	@Autowired
	private PrepaidService prepaidService;

	@Autowired
	private SpecialistService specialistService;

	@Autowired
	private PatientModel model;

	/**
	 * Phone Events controller
	 */
	public void addPhoneEvent() {
		model.setEditedPhone(new Phone());
	}

	/**
	 * Prepaid Events controller
	 */
	public void addPrepaidEvent() {
		model.setEditedPatientPrepaid(new PatientPrepaid());
	}

	@Override
	protected void bindEntityToModel() {
		Patient editedPatient = model.getEditedEntity();
		model.setFirstName(editedPatient.getFirstName());
		model.setLastName(editedPatient.getLastName());
		model.setBirthDate(editedPatient.getBirthDate());
		model.setDocumentType(editedPatient.getDocumentType());
		model.setDocumentNumber(editedPatient.getDocumentNumber());
		model.setAddress(editedPatient.getAddress());
	}

	@Override
	protected void bindModelToEntity() {
		Patient editedPatient = model.getEditedEntity();
		editedPatient.setFirstName(model.getFirstName());
		editedPatient.setLastName(model.getLastName());
		editedPatient.setBirthDate(model.getBirthDate());
		editedPatient.setDocumentType(model.getDocumentType());
		editedPatient.setDocumentNumber(model.getDocumentNumber());
		editedPatient.setAddress(model.getAddress());
	}

	public void confirmDeletePhoneEvent() {
		model.getEditedEntity().getPhones().remove(model.getEditedPhone());
	}

	public void confirmDeletePrepaidEvent() {
		model.getEditedEntity().getPrepaids().remove(model.getEditedPatientPrepaid());
	}

	public void confirmSavePhoneEvent() {
		Phone editedPhone = model.getEditedPhone();

		for (Phone phone : model.getEditedEntity().getPhones()) {
			if (phone.getUid().equals(model.getEditedPhone().getUid())) {
				phone.setAreaCode(editedPhone.getAreaCode());
				phone.setComment(editedPhone.getComment());
				phone.setCountryCode(editedPhone.getCountryCode());
				phone.setPhoneNumber(editedPhone.getPhoneNumber());
				return;
			}
		}
		model.getEditedEntity().getPhones().add(model.getEditedPhone());
	}

	public void confirmSavePrepaidEvent() {
		PatientPrepaid editedPatientPrepaid = model.getEditedPatientPrepaid();

		for (PatientPrepaid prepaid : model.getEditedEntity().getPrepaids()) {
			if (prepaid.getUid().equals(model.getEditedPatientPrepaid().getUid())) {
				prepaid.setAssociateNumber(editedPatientPrepaid.getAssociateNumber());
				prepaid.setPrepaid(editedPatientPrepaid.getPrepaid());
				return;
			}
		}
		model.getEditedEntity().getPrepaids().add(model.getEditedPatientPrepaid());
	}

	protected Class<Patient> getEntityClass() {
		return Patient.class;
	}

	@Override
	protected AbstractModel<Patient> getModel() {
		return model;

	}

	@Override
	protected Service<Patient> getService() {
		return service;
	}

	public void init() {
		super.init();
		model.setFilter(new PatientFilter());
		populateNonEntityPropertiesToModel();
	}

	public String onFlowProcess(FlowEvent event) {
		return event.getNewStep();
	}

	@Override
	public void populateNonEntityPropertiesToModel() {
		model.setSelectableCities(cityService.getList(new CityFilter()));
		model.setSelectablePrepaids(prepaidService.getList(new PrepaidFilter()));
		model.setSelectableSpecialists(specialistService.getList(new SpecialistFilter()));
	}
}
